package com.javasm.mingming.adminuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.mingming.adminuser.dao.AdminUserDao;
import com.javasm.mingming.adminuser.entity.AdminAuthority;
import com.javasm.mingming.adminuser.entity.AdminRole;
import com.javasm.mingming.adminuser.entity.AdminUser;
import com.javasm.mingming.adminuser.service.AdminAuthorityService;
import com.javasm.mingming.adminuser.service.AdminRoleService;
import com.javasm.mingming.adminuser.service.AdminUserService;
import com.javasm.mingming.common.exception.JavasmException;
import com.javasm.mingming.common.exception.JavasmExceptionEnum;
import com.javasm.mingming.login.entity.LoginUserDetails;
import com.javasm.mingming.login.service.impl.UsernameUserDetailsService;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 后台用户(AdminUser)表服务实现类
 *
 * @author makejava
 * @since 2025-01-11 09:51:34
 */
@Service("adminUserService")
public class AdminUserServiceImpl extends ServiceImpl<AdminUserDao, AdminUser> implements AdminUserService {

    @Resource
    AdminRoleService roleService;
    @Resource
    AdminAuthorityService authorityService;

    @Override
    public AdminUser queryMyUser() {
        //从Security中获取 用户信息
        //获取上下文对象
        SecurityContext context = SecurityContextHolder.getContext();
        //包含用户信息 UsernamePasswordAuthenticationToken
        Authentication authentication = context.getAuthentication();
        if (authentication == null){
            throw new JavasmException(JavasmExceptionEnum.NotLogin);
        }
        //已经登录  UserDetails
        Object principal = authentication.getPrincipal();
        LoginUserDetails loginUserDetails = (LoginUserDetails) principal;
        AdminUser adminUser = loginUserDetails.getAdminUser();
        return adminUser;
    }

    @Resource
    PasswordEncoder passwordEncoder;

    @Override
    public void updateAdminUser(AdminUser adminUser) {
        //获取,现在已经登录的用户信息
        AdminUser loginUser = queryMyUser();//Security中获取
        //修改数据库的信息
        //可能被修改的值
        if (adminUser.getPassword() != null){
            //修改密码
            String s = passwordEncoder.encode(adminUser.getPassword());
            loginUser.setPassword(s);
        }
        if (adminUser.getRoleId() != null){
            loginUser.setRoleId(adminUser.getRoleId());
        }
        if (adminUser.getIsvalid() != null){
            loginUser.setIsvalid(adminUser.getIsvalid());
        }
        if (adminUser.getRemark() != null){
            loginUser.setRemark(adminUser.getRemark());
        }
        if (adminUser.getHeadImg() != null){
            loginUser.setHeadImg(adminUser.getHeadImg());
        }
        updateById(loginUser);
        //数据库更新结束
        //修改security中的用户信息
        //模仿源码中,把Authentication重新new出来
        LoginUserDetails loginUserDetails = new LoginUserDetails(loginUser);
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(
                        loginUserDetails,
                        loginUserDetails.getPassword(),
                        loginUserDetails.getAuthorities()
                );
        //重新放回上下文对象
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
    }
    @Override
    public AdminUser getByUname(String username) {
        LambdaQueryWrapper<AdminUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminUser::getUsername,username);
        AdminUser adminUser = getOne(queryWrapper);
        if (adminUser != null){
            //根据 rid 查询 角色信息
            AdminRole adminRole = roleService.getByRid(adminUser.getRoleId());
            adminUser.setAdminRole(adminRole);
            //查询 权限列表
            List<AdminAuthority> authorityList = authorityService.getByUid(adminUser.getUid());
            adminUser.setAuthorityList(authorityList);
        }
        return adminUser;
    }
    @Override
    public AdminUser getByPhone(String phone) {
        LambdaQueryWrapper<AdminUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminUser::getPhone,phone);
        AdminUser adminUser = getOne(queryWrapper);
        return adminUser;
    }

    @Override
    public AdminUser getByUid(String uid) {
        AdminUser adminUser = getById(Long.parseLong(uid));
        return adminUser;
    }
}

